//
//  TKFriendListSetCell.m
//  TKIMSDK
//
//  Created by TryHone on 16/6/29.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "TKFriendListSetCell.h"
//#import "TKHttpService.h"
//#import "TKIMUser+Msgtypeid.h"


@implementation TKFriendListSetCell{

    UILabel* _line;

}




+(instancetype)cellWithTableView:(UITableView *)table{
    static NSString *ID = @"TKFriendListSetCell";
    TKFriendListSetCell *cell = [table dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[TKFriendListSetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self buildUI];
        [self layoutUI];
    }
    return self;
}

//构建UI界面
-(void)buildUI{
    [self.contentView addSubview:self.username];
    [self.contentView addSubview:self.accept];
      [self.contentView addSubview:[self line]];
    
    
}
//设置ui约束
-(void)layoutUI{
    CGFloat magin=10;
    CGFloat nametopMargin=15;
    CGFloat acceptwidth=50;
    CGFloat accepttopmargin=7;
    
    _username.frame=CGRectMake(magin, nametopMargin, (self.width-acceptwidth-3*magin), self.height-2*nametopMargin);
    _accept.frame=CGRectMake(CGRectGetMaxX(_username.frame)+magin, accepttopmargin, acceptwidth, self.height-2*accepttopmargin);
    
    _line.frame=CGRectMake(magin, self.height-1, self.width-magin, 1);
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self layoutUI];
}


#pragma mark--懒加载
-(UILabel*)username{
    if (_username==nil) {
        _username=[[UILabel alloc] init];
    }
    return _username;
}
-(UILabel*)line{
    if (_line==nil) {
        _line=[[UILabel alloc] init];
        _line.backgroundColor=[TKUIHelper colorWithHexString:@"#e5e5e5"];
    }
    return _line;
}

-(UISwitch*)accept{

    if (_accept==nil) {
        _accept=[[UISwitch alloc] init];
        _accept.onTintColor=[TKUIHelper colorWithHexString:@"#d5effe"];
        [_accept addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _accept;
}

-(void)setUser:(TKIMUser *)user{
    
    if (user) {
        _user=user;
        
        _username.text=[TKIMHelper getShowNameWithUser:_user];
        if (_user.isAcceptPush) {
            _accept.on=YES;
        }else{
            _accept.on=NO;
        
        
        }
    }

}

-(void)switchAction:(UISwitch*)swith{

    BOOL isButtonOn = [swith isOn];
    
    NSString* msgtypeid=_user.msgtypeid;
    NSString* appkey=[TKHttpService app_key];
    NSString* cust_code=[TKHttpService cust_code];
    NSString* state=[NSString stringWithFormat:@"%d",!isButtonOn];
    [[[TKHttpService alloc] init] setAccpetMsg:appkey cust_code:cust_code msgtypeid:msgtypeid state:state callBack:^(TKIMResultVo *resultVo) {
        
        if (resultVo.errorNo==0) {
              NSLog(@"推送设置正确");
            _user.isAcceptPush=isButtonOn;
            TKIMUser * tempuser = [[TKIMDBManager shareInstance] queryAHisUserByUserId:_user.userID];
            if (tempuser) {
                tempuser.isAcceptPush = _user.isAcceptPush;
                [[TKIMDBManager shareInstance]updateUserIsAcceptPush:tempuser];
            }else{
                [[TKIMDBManager shareInstance] insertUser:_user];
            }
            
           
        }else{
          [[TKIMLayerUtils instance] showTip:resultVo.errorInfo];
            swith.on=!isButtonOn;
        }
    }];
    
   
    

}


#pragma mark--系统生成
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
